var thisPage = {
    GetFilter: function (start, end) {
        var _params = {};
        _params["UserId"] = $("#UserId").val();
        _params["Start"] = $.fullCalendar.moment(start).format("YYYY-MM-DD HH:mm:ss");
        _params["End"] = $.fullCalendar.moment(end).format("YYYY-MM-DD HH:mm:ss");

        return JSON.stringify(_params);
    },
    FetchCalendar: function (start, end, callback) {
        $.ajax({
            type: "POST",
            url: parent.basePath + "/oa/mycalendar/fetchCalendar.html",
            timeout: 30 * 1000,
            data: thisPage.GetFilter(start, end),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            var events = [];
            for (var i = 0; i < data.length; i++) {
                var plan = {};
                plan['id'] = data[i].calendarId;
                plan['title'] = data[i].title;
                plan['start'] = new Date(Date.parse(data[i].startTime));
                plan['backgroundColor'] = data[i].backgroundColor;
                plan['borderColor'] = data[i].borderColor;
                plan['textColor'] = data[i].textColor;
                if (parseInt(data[i].allDay) === 0) {
                    plan['allDay'] = false;
                    plan['end'] = new Date(Date.parse(data[i].endTime));
                } else {
                    plan['allDay'] = true;
                }
                events.push(plan);
            }
            callback(events);
        }
        ).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(function () {
            $('#loading').hide();
        });
    },
    SetCalendar: function (flag, date, calendarId) {
        if (flag === 0) {
            if (date !== null) {
                showModal(750, 450, "新建", parent.basePath + "/oa/mycalendar/edit.html?date=" + date, null, function ($dialog) {
                    var _exist = $.inArray($dialog, parent.arrDialog);
                    if (_exist >= 0) {
                        parent.arrDialog.splice($.inArray($dialog, parent.arrDialog), 1);
                    }
                });
            } else {
                showModal(750, 450, "新建", parent.basePath + "/oa/mycalendar/edit.html", null, function ($dialog) {
                    var _exist = $.inArray($dialog, parent.arrDialog);
                    if (_exist >= 0) {
                        parent.arrDialog.splice($.inArray($dialog, parent.arrDialog), 1);
                    }
                });
            }
        }
        if (flag === 1) {
            showModal(750, 450, "编辑", parent.basePath + "/oa/mycalendar/edit.html?calendarId=" + calendarId, null, function ($dialog) {
                var _exist = $.inArray($dialog, parent.arrDialog);
                if (_exist >= 0) {
                    parent.arrDialog.splice($.inArray($dialog, parent.arrDialog), 1);
                }
            });
        }
    }
};
//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#form_data").data("changed", false); // 初始化form数据提交状态
    let _defaultDate = getCurrentDate();    // 设置默认日期为今日

    $("#calendar").fullCalendar({
        customButtons: {
            butSetCalendar: {
                text: "新建",
                click: function () {
                    thisPage.SetCalendar(0, null, null);
                }
            }
        },
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'butSetCalendar,month,agendaWeek,agendaDay,listMonth'
        },
        height: "parent",
        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthNamesShort: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
        dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
        today: ["今天"],
        firstDay: 0,
        buttonIcons: {
            prev: 'left-single-arrow',
            next: 'right-single-arrow',
            prevYear: 'left-double-arrow',
            nextYear: 'right-double-arrow'
        },
        buttonText: {
            today: '今天',
            month: '月',
            week: '周',
            day: '日',
            listMonth: '列表'
        },
        slotLabelFormat: "H",
        businessHours: {
            dow: [1, 2, 3, 4, 5],
            start: "8:00",
            end: "18:00"
        },
        allDaySlot: true,
        allDayText: "全天",
        timeFormat: "HH:mm", // 设置的是添加的具体的日程上显示的时间
        views: {
            month: {
                titleFormat: "YYYY年M月"
            },
            week: {
                titleFormat: "YYYY年M月D日",
                columnFormat: "M.D dddd"
            },
            day: {
                titleFormat: "YYYY年M月D日 dddd",
                columnFormat: "M/D dddd"
            }
        },
        defaultDate: _defaultDate,
        editable: true, // 允许拖动 
        // 设置拖动时事件的透明度
        dragOpacity: {
            agenda: .5,
            '': .6
        },
        eventLimit: true, // 当事件太多的时候出现 "more" 
        events: function (start, end, timezone, callback) {
            thisPage.FetchCalendar(start, end, callback);
        },
        // 拖动事件 
        eventDrop: function (event, delta, revertFunc, jsEvent, ui, view) {
            var _params = {};
            _params['id'] = event.id;
            _params['daysDiff'] = delta._days;                   // 拖动后相差的天数
            _params['millisecondsDiff'] = delta._milliseconds;   // 拖动后差的时间
            _params['allDay'] = event.allDay === true ? 1 : 0;
            //alert(event.title + ' was moved ' + delta + ' days\n' + '(should probably update your database)');
        },
        // 重设置结束时间
        eventResize: function (event, delta, revertFunc, jsEvent, ui, view) {
            //alert(event.title + " end is now " + event.end.format());
        },
        // 事件点击操作：编辑操作（包括删除)
        eventClick: function (event, jsEvent, view) {
            thisPage.SetCalendar(1, null, event.id);
        },
        // 日程点击：添加日程  
        dayClick: function (date, allDay, jsEvent, view) {
            thisPage.SetCalendar(0, $.fullCalendar.moment(date).format("YYYY-MM-DD HH:mm:ss"), null);
        }
    });
}

function onPageResize() {
    let h = $(window).height();
    $("#myContainer").css({height: h - 69});
}
